package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.SystemDictionaryItem;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.query.SystemDictionaryQuery;
import cn.wolfcode.crm.service.ISystemDictionaryItemService;
import cn.wolfcode.crm.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/systemDictionaryItem")
public class SystemDictionaryItemController {

    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;

    // 提供一个方法处理查询所有部门数据请求，响应 HTML
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") SystemDictionaryQuery qo) {
        //调用service的方法的到结果集
        PageInfo pageInfo = systemDictionaryItemService.query(qo);

        //查询所有的字典目录共享
        model.addAttribute("systemDictionarys", systemDictionaryItemService.listAll());

        //共享结果集：在页面遍历显示
        model.addAttribute("result", pageInfo);
        return "systemDictionaryItem/list"; //返回视图名称：加上前缀和后缀的ftl
    }


    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(SystemDictionaryItem systemDictionaryItem) {
        if (systemDictionaryItem.getId() == null) { // 代表新增
            systemDictionaryItemService.save(systemDictionaryItem);
        } else { // 代表修改
            systemDictionaryItemService.update(systemDictionaryItem);
        }
        return new JsonResult();
    }


    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        //int i = 1/0;
        systemDictionaryItemService.delete(id);
        return new JsonResult();
    }
}
